package org.openfans.web;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.openfans.domain.Facade;
import org.openfans.domain.Fans;
import org.openfans.domain.FeedBack;
import org.openfans.domain.PreFans;
import org.openfans.util.ThreadFans;
import org.springframework.validation.BindException;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;

public class FeedBackFormController extends SimpleFormController {
	private Facade facade = null;

	public void setFacade(Facade facade) {
		this.facade = facade;
	}

	public FeedBackFormController() {
		setCommandName("feedBack");
		setCommandClass(FeedBack.class);
	}

	protected ModelAndView onSubmit(HttpServletRequest request,
			HttpServletResponse response, Object command, BindException errors)
			throws Exception {
		FeedBack feedBack = (FeedBack) command;
		feedBack.setSubmitDate(new Date());
		facade.saveFeedBack(feedBack);
		String message = "谢谢您的反馈，我们会认真考虑您的建议！";
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("message", message);

		return new ModelAndView("success", map);
	}

	protected Object formBackingObject(HttpServletRequest request)
			throws Exception {
		Fans fans = ThreadFans.get();
		if (!(fans instanceof PreFans)) {
			return new FeedBack(fans);
		} else {
			return new FeedBack();
		}

	}
}
